package com.arpa.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import com.arpa.ntocc.common.common.exception.ServiceException;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.cache.UnitCache;
import com.arpa.wms.domain.entity.*;
import com.arpa.wms.domain.enums.WmsTaskTypeEnum;
import com.arpa.wms.mapper.WmsTaskItemMapper;
import com.arpa.wms.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.arpa.wms.domain.consts.CodeConst.*;

/**
 * <p>
 * 任务明细 Service实现类
 * </p>
 *
 * @author LEO
 * @since 2020-10-09
 */
@Service
public class WmsTaskItemServiceImpl extends ServiceImpl<WmsTaskItemMapper, WmsTaskItem> implements IWmsTaskItemService {


    private final IReceiveItemService receiveItemService;

    private final IPutawayItemService putawayItemService;

    private final IPickingItemService pickingItemService;

    private final ICountingRegisterService countingRegisterService;

    private final IMoveItemService moveItemService;

    private final IOutboundItemService outboundItemService;

    private final UnitCache unitCache;

    public WmsTaskItemServiceImpl(IReceiveItemService receiveItemService,
                                  IPutawayItemService putawayItemService,
                                  IPickingItemService pickingItemService,
                                  IMoveItemService moveItemService,
                                  IOutboundItemService outboundItemService,
                                  ICountingRegisterService countingRegisterService,
                                  UnitCache unitCache
    ) {
        this.receiveItemService = receiveItemService;
        this.putawayItemService = putawayItemService;
        this.pickingItemService = pickingItemService;
        this.moveItemService = moveItemService;
        this.outboundItemService = outboundItemService;
        this.countingRegisterService = countingRegisterService;
        this.unitCache = unitCache;
    }


    /**
     * 获取任务明细
     * @param taskType 任务类型
     * @param taskCode 任务code
     * @return
     */
    @Override
    public Object getTaskDetails(final WmsTaskTypeEnum taskType, final String taskCode, final String sourceCode) {

        List<WmsTaskItem> wmsTaskItems = list(new QueryWrapper<WmsTaskItem>().lambda().eq(WmsTaskItem::getTaskCode, taskCode).eq(WmsTaskItem::getGroupCode,UserUtil.getBranchCode()));
        if(IterUtil.isNotEmpty(wmsTaskItems)) {
            List<String> sourceCodes = wmsTaskItems.stream().map(WmsTaskItem::getSourceCode).collect(Collectors.toList());

            switch (taskType) {
                case PUTAWAY: {
                    //上架
                    List<PutawayItem> items = putawayItemService.list(new QueryWrapper<PutawayItem>().lambda().in(PutawayItem::getCode, sourceCodes).eq(PutawayItem::getGroupCode, UserUtil.getBranchCode()));

                    List<Map<String,Object>> itemMaps = items.stream().map(e->{
                        Map<String,Object> item = BeanUtil.beanToMap(e);
                        item.put(GOODS_UNIT_NAME, e.getGoodsUnitName());
                        item.put(PLAN_QUANTITY,e.getReceivedQuantity());
                        item.put(JOB_QUANTITY,e.getPutawayQuantity());
                        return item;
                    }).collect(Collectors.toList());

                    return itemMaps;
                }

                case MOVE: {
                    //移位
                    List<MoveItem> items = moveItemService.list(new QueryWrapper<MoveItem>().lambda()
                            .in(MoveItem::getCode, sourceCodes)
                            .eq(MoveItem::getGroupCode,UserUtil.getBranchCode()));

                    List<Map<String,Object>> itemMaps = items.stream().map(e->{
                        Map<String,Object> item = BeanUtil.beanToMap(e);
                        item.put(GOODS_UNIT_NAME, unitCache.translate(e.getGoodsUnit()));
                        item.put(PLAN_QUANTITY,e.getMoveQuantity());
                        item.put(JOB_QUANTITY,e.getMovedQuantity());
                        return item;
                    }).collect(Collectors.toList());
                    return itemMaps;
                }
                case INVENTORY: {
                    //盘点
                    List<CountingRegister> items = countingRegisterService.list(new QueryWrapper<CountingRegister>().lambda().in(CountingRegister::getCountingItemCode, sourceCodes));
                    List<Map<String,Object>> itemMaps = items.stream().map(e->{
                        Map<String,Object> item = BeanUtil.beanToMap(e);
                        item.put(GOODS_UNIT_NAME, e.getGoodsUnitName());
                        item.put(PLAN_QUANTITY,e.getInventoryQuantity());
                        item.put(JOB_QUANTITY,e.getCountingQuantity());
                        return item;
                    }).collect(Collectors.toList());
                    return itemMaps;
                }
                default:
                    throw new ServiceException("任务类型错误");
            }
        } else {
            switch (taskType) {
                case PICKING: {
                    //拣货
                    List<PickingItem> items = pickingItemService.list(new QueryWrapper<PickingItem>().lambda()
                            .in(PickingItem::getPickingCode, sourceCode)
                            .eq(PickingItem::getGroupCode,UserUtil.getBranchCode()));

                    List<Map<String,Object>> itemMaps = items.stream().map(e->{
                        Map<String,Object> item = BeanUtil.beanToMap(e);
                        item.put(GOODS_UNIT_NAME, e.getGoodsUnitName());
                        item.put(PLAN_QUANTITY,e.getDistributionQuantity());
                        item.put(JOB_QUANTITY,e.getPickingQuantity());
                        return item;
                    }).collect(Collectors.toList());
                    return itemMaps;
                }
                case RECEIVE: {
                    //收货
                    List<ReceiveItem> items = receiveItemService.list(new QueryWrapper<ReceiveItem>().lambda()
                            .eq(ReceiveItem::getReceiveCode, sourceCode)
                            .eq(ReceiveItem::getGroupCode,UserUtil.getBranchCode()));

                    List<Map<String, Object>> itemMaps = items.stream().map(e -> {
                        Map<String, Object> item = BeanUtil.beanToMap(e);
                        item.put(GOODS_UNIT_NAME, e.getGoodsUnitName());
                        item.put(PLAN_QUANTITY, e.getPlanQuantity());
                        item.put(JOB_QUANTITY, e.getReceivedQuantity());
                        return item;
                    }).collect(Collectors.toList());

                    return itemMaps;
                }
                case CHECK: {
                    //复核
                    List<OutboundItem> items = outboundItemService.list(new QueryWrapper<OutboundItem>().lambda()
                            .eq(OutboundItem::getOutboundCode, sourceCode)
                            .eq(OutboundItem::getGroupCode,UserUtil.getBranchCode()));

                    List<Map<String, Object>> itemMaps = items.stream().map(e -> {
                        Map<String, Object> item = BeanUtil.beanToMap(e);
                        item.put(GOODS_UNIT_NAME, e.getGoodsUnitName());
                        item.put(PLAN_QUANTITY, e.getPlanQuantity());
                        item.put(JOB_QUANTITY, e.getRecheckQuantity());
                        return item;
                    }).collect(Collectors.toList());
                    return itemMaps;
                }
                default:
                    throw new ServiceException("任务类型错误");
            }
        }
    }
}
